<template>
  <div class="home-tool">
    <div class="search">
      <ul class="top">
        <li
          :class="['top-tab', { active: activeIndex === itemIndex }]"
          v-for="(item, itemIndex) in tabs"
          :key="itemIndex"
          @click="changeTab(itemIndex)"
        >
          <span
            :class="[
              'icon',
              activeIndex === itemIndex ? item.iconA : item.icon,
            ]"
          ></span>
          <p class="text">{{ item.name }}</p>
        </li>
      </ul>
      <div class="content">
        <ol class="content-tabs">
          <li class="content-tab" v-show="activeIndex === 0">
            <el-autocomplete
              class="inp"
              v-model="searchVal"
              :fetch-suggestions="querySearchAsync"
              placeholder="请输入搜索内容"
              @select="handleSearch"
            >
              <template v-slot:prepend>
                <span class="icon icon-pre-search"></span>
              </template>
            </el-autocomplete>
          </li>
          <li class="content-tab tab-select" v-show="activeIndex === 1">
            <div class="content-sels">
              <el-select
                class="content-sel"
                v-model="selParams.province"
                filterable
                placeholder="省份"
                @change="changeProvince"
              >
                <el-option
                  v-for="item in provinceList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
              <el-select
                class="content-sel"
                v-model="selParams.basin"
                filterable
                placeholder="流域"
                @change="changeBasin"
              >
                <el-option
                  v-for="(item, itemIndex) in basinList"
                  :key="itemIndex"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
              <el-select
                class="content-sel"
                v-model="selParams.county"
                filterable
                placeholder="区县"
                @change="changeCounty"
              >
                <el-option
                  v-for="item in countyList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
              <el-select
                class="content-sel"
                v-model="selParams.village"
                filterable
                placeholder="村庄"
                @change="changeVillage"
              >
                <el-option
                  v-for="item in villageList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
            <el-button class="content-btn" @click="resetSelParams"
              >重置</el-button
            >
          </li>
        </ol>
      </div>
    </div>
    <div class="info-house-box">
      <div class="title">中国农房基本情况</div>
      <div class="row">
        <div class="label">农房栋数：</div>
        <div class="value">
          {{ $_config.NationaData.count_sum }}<span>亿栋</span>
        </div>
      </div>
      <div class="row">
        <div class="label">重置市辖区：</div>
        <div class="value">
          {{ $_config.NationaData.count_shixiaqu }}<span>亿栋</span>
        </div>
      </div>
      <div class="row">
        <div class="label">县级市：</div>
        <div class="value">
          {{ $_config.NationaData.count_xianjishi }}<span>亿栋</span>
        </div>
      </div>
      <div class="row">
        <div class="label">县（旗）：</div>
        <div class="value">
          {{ $_config.NationaData.count_xian }}<span>亿栋</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { ElMessage } from 'element-plus'
import config from "config";
import baiduMapServer from "utils/baiduMapServer";
import coorTransformation from "utils/coorTransformation";
const $_config = ref(config);
console.log($_config.value);
const activeIndex = ref(0);
const tabs = ref([
  { icon: "icon-inp", iconA: "icon-inp-a", name: "模糊搜索" },
  { icon: "icon-sel", iconA: "icon-sel-a", name: "快速导航" },
]);
const searchVal = ref("");
const selParams = ref({
  province: "",
  basin: "",
  county: "",
  village: "",
});
const provinceList = ref([]);
const basinList = ref([]);
const countyList = ref([]);
const timeout = ref(null);
const villageList = ref([]);
const initProvince = () => {
  let data = $_config.value.NATIONAL_DATA;
  let list = [];
  data.forEach((item) => {
    const obj = {
      label: item.province,
      value: item.province_code,
    };
    const index = list.findIndex(
      (listItem) => listItem.label === item.province
    );
    if (index === -1) {
      list.push(obj);
    }
  });
  provinceList.value = list;
};
const initBasin = (code) => {
  let data = $_config.value.NATIONAL_DATA;
  let list = [];
  data.forEach((item) => {
    if (code === item.province_code) {
      const obj = {
        label: item.region,
        value: item.region + item.province_code,
      };
      const index = list.findIndex(
        (listItem) => listItem.label === item.region
      );
      if (index === -1) {
        list.push(obj);
      }
    }
  });
  basinList.value = list;
};
const initCounty = (label) => {
  let data = $_config.value.NATIONAL_DATA;
  let list = [];
  data.forEach((item) => {
    if (label === item.region + item.province_code) {
      const obj = {
        label: item.county,
        value: item.county + item.province_code,
      };
      const index = list.findIndex(
        (listItem) => listItem.label === item.county
      );
      if (index === -1) {
        list.push(obj);
      }
    }
  });
  countyList.value = list;
};
const initVillage = (label) => {
  let data = $_config.value.NATIONAL_DATA;
  let list = [];
  data.forEach((item) => {
    if (label === item.county + item.province_code) {
      const obj = {
        label: item.village,
        value: item.village + item.province_code,
      };
      const index = list.findIndex(
        (listItem) => listItem.label === item.village
      );
      if (index === -1) {
        list.push(obj);
      }
    }
  });
  villageList.value = list;
};
const changeTab = (index) => {
  activeIndex.value = index;
};
const querySearchAsync = (queryString, cb) => {
  onSearch(queryString, cb);
};

const handleSearch = (val) => {
  console.log(val);
};

const onSearch = (value, cb) => {
  if (value) {
    baiduMapServer.suggestion({ query: value }).then((res) => {
      if (res && res.code == 200 && res.data) {
        let data = JSON.parse(res.data);
        if (data && data.status == 0 && data.result && data.result.length > 0) {
          const results = data.result.map((item) => {
            return {
              address: item.address,
              value: item.name,
            };
          });
          console.log(results);
          cb(results);
          //处理经纬度转换（百度->wgs84）
          for (let item of data.result) {
            clearTimeout(timeout.value);
            if (
              item &&
              item.location &&
              item.location.lng &&
              item.location.lat
            ) {
              //经纬度转换（百度->wgs84）
              let wgs84Coor = coorTransformation.bd09towgs84(
                item.location.lng,
                item.location.lat
              );
              item.location.lng = wgs84Coor[0];
              item.location.lat = wgs84Coor[1];
            }
          }
        }
      } else {
        ElMessage.error(res.msg ? res.msg : "获取数据失败");
      }
    });
  } else {
  }
};

const changeProvince = (val) => {
  console.log(typeof val, "changeProvince");
  selParams.value.basin = "";
  selParams.value.county = "";
  selParams.value.village = "";
  basinList.value = [];
  countyList.value = [];
  villageList.value = [];
  initBasin(val);
};

const changeBasin = (val) => {
  console.log(val, "changeBasin");
  selParams.value.county = "";
  selParams.value.village = "";
  countyList.value = [];
  villageList.value = [];
  initCounty(val);
};
const changeCounty = (val) => {
  console.log(val, "changeCounty");
  selParams.value.village = "";
  villageList.value = [];
  initVillage(val);
};
const changeVillage = (val) => {
  console.log(val, "changeVillage");
};
const resetSelParams = () => {
  selParams.value.province = "";
  selParams.value.basin = "";
  selParams.value.county = "";
  selParams.value.village = "";
  basinList.value = [];
  countyList.value = [];
  villageList.value = [];
};
onMounted(() => {
  initProvince();
});
</script>

<style lang="scss" scoped>
.search {
  position: absolute;
  left: 14px;
  top: 17px;
  width: 500px;
  padding: 0 23px 23px 23px;
  box-sizing: border-box;
  min-height: 131px;
  z-index: 99;
  background: url("@/assets/tool/search-bg.png") no-repeat 0 0;
  background-size: 100%;
  .top {
    display: flex;
    width: 100%;
    padding-top: 10px;
    height: 56px;
    .top-tab {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      .icon {
        display: inline-block;
        width: 22px;
        height: 22px;
        &.icon-inp {
          background: url("@/assets/tool/icon-inp.png") no-repeat 0 0;
          background-size: 100%;
        }
        &.icon-inp-a {
          background: url("@/assets/tool/icon-inp-a.png") no-repeat 0 0;
          background-size: 100%;
        }
        &.icon-sel {
          background: url("@/assets/tool/icon-sel.png") no-repeat 0 0;
          background-size: 100%;
        }
        &.icon-sel-a {
          background: url("@/assets/tool/icon-sel-a.png") no-repeat 0 0;
          background-size: 100%;
        }
      }
      .text {
        margin-left: 10px;
        font-size: 16px;
        font-family: Source Han Sans CN;
        font-weight: bold;
        color: rgba(255, 255, 255, 0.8);
      }
      &.active {
        position: relative;
        .text {
          color: #4ceab9;
        }
        &::after {
          content: "";
          position: absolute;
          left: 0;
          bottom: 0;
          width: 100%;
          height: 2px;
          background: rgba(76, 234, 185, 0.6);
          border-radius: 0.25px;
        }
      }
    }
  }
  .content {
    margin-top: 16px;
    width: 100%;
    .content-tabs {
      .content-tab {
        .inp :deep(.el-input__inner) {
          color: #fff;
        }
        .inp :deep(.el-input-group__prepend) {
          background-color: rgba(6, 47, 54, 0.3);
          border: 1px solid rgba(6, 47, 54, 0.3);
        }
        .icon-pre-search {
          display: flex;
          width: 20px;
          height: 20px;
          background: url("@/assets/tool/icon-search.png") no-repeat 0 0;
          background-size: 100%;
        }
      }
      .content-tab :deep(.el-autocomplete) {
        width: 100%;
      }
      .content-tab :deep(.el-input__inner) {
        color: #fff;
        background: rgba(6, 47, 54, 0.3);
        border: 1px solid rgba(6, 47, 54, 0.3);
      }
      .content-tab :deep(.el-input.is-active .el-input__inner),
      .el-input__inner:focus {
        background: rgba(6, 47, 54, 0.3);
      }
      .content-sels {
        display: flex;
        .content-sel {
          &:first {
            margin-left: 0;
          }
          margin-left: 10px;
        }
      }
    }
    .tab-select {
      padding-bottom: 46px;
      .content-btn {
        position: absolute;
        right: 40px;
        bottom: 23px;
        width: 90px;
        height: 30px;
        background: rgba(255, 255, 255, 0.25);
        border-radius: 5px;
        font-size: 14px;
        font-family: Source Han Serif SC;
        font-weight: 300;
        color: #ffffff;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }
  }
}
.info-house-box {
  position: absolute;
  bottom: 30px;
  right: 30px;
  pointer-events: auto;
  width: 360px;
  height: 200px;
  padding: 20px;
  z-index: 99;
  .title {
    margin-left: 20px;
    font-size: 20px;
    font-weight: 700;
  }
  .row {
    margin: 10px 20px;
    height: 32px;
    line-height: 32px;
    font-size: 20px;
    font-weight: bold;
    color: #000;
    display: flex;
    .label {
      width: 120px;
    }
    .value {
      span {
        display: inline-block;
        color: #000;
        font-size: 18px;
        font-weight: bold;
      }
    }
  }
}
</style>
